package com.example.demo.service;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;


    public Object findAll(Student student) {
        if("".equals(student.getName())){
            student.setName(null);
        }
        return studentRepository.findAll(Example.of(student));
    }

    public void deleteById(String id) {
        studentRepository.deleteById(id);
    }

    public void save(Student s) {
        if(s.getId()==null){
            s.setId(UUID.randomUUID().toString());
        }

        studentRepository.save(s);
    }

    public Student getReferenceById(String id) {
        return studentRepository.getReferenceById(id);
    }
}
